package org.one.stone.soup.grfxML;

import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class DataLine extends DataGeometry implements ITag {
	public static String DEFAULT_ID = "_Line";
		DataPoint start = new DataPoint();
		DataPoint startView = new DataPoint();
		DataPoint end = new DataPoint();
		DataPoint endView = new DataPoint();
		DataPoint middle = new DataPoint();
		DataSize thickness = new DataSize(1);
		DataSize thicknessView = new DataSize(1);

		GeometryLine _line = null;
		DataTranslation _translation;
public DataLine() {
	try{
		setId(DEFAULT_ID,this);
	}
	catch(LockException le){} //Dummy catch
}
public void buildFromgrfxML(XmlElement xml)
{

	String name = xml.getAttributeValueByName(Token.ID);
	if(name!=null)
	{
		try{
			setId(name,this);
		}
		catch(LockException le){} //Dummy catch
	}

	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START))
		{
			start = new DataPoint(TagModifier.POINT_TYPE_POINT);
			start.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.END))
		{
			end = new DataPoint(TagModifier.POINT_TYPE_POINT);
			end.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.MIDDLE))
		{
			middle = new DataPoint(TagModifier.POINT_TYPE_POINT);
			middle.buildFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.THICKNESS))
		{
			thickness = new DataSize(TagModifier.SIZE_TYPE_THICKNESS);
			thickness.buildFromgrfxML(element,TagModifier.SIZE_TYPE_THICKNESS);
		}
		else if(super.buildFromElement(element)==false && !NameHelper.isSystemName(element.getName()))
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.LINE,""+element.getStartLineNo()} );
		}
	}
}
public void copyFromgrfxML(XmlElement xml)
{
	XmlElement element;

	for(int loop=0;loop<xml.getElementCount();loop++)
	{
		element = xml.getElementByIndex(loop);
		if(element == null)
			continue;

		String eleName = element.getName();

		if(eleName.equals(Token.START))
		{
			start.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.END))
		{
			end.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.MIDDLE))
		{
			middle.copyFromgrfxML(element,TagModifier.POINT_TYPE_POINT);
		}
		else if(eleName.equals(Token.THICKNESS))
		{
			thickness.copyFromgrfxML(element,TagModifier.SIZE_TYPE_THICKNESS);
		}
		else if(super.copyFromElement(element)==false)
		{
			GrfxMLException.warning( GrfxMLException.WARNING_UNRECOGNISED_TAG_CODE ,new Object[]{eleName,Token.LINE,""+element.getStartLineNo()} );
		}
	}
}
public DataGeometry getClone() {

	DataLine newLine = new DataLine();

	super.getClonedElements(newLine);

	newLine.start = start.getClone();
	newLine.end = end.getClone();
	newLine.middle = middle.getClone();
	newLine.thickness = thickness.getClone();

	return newLine;
}
public DataGeometry getClone(String path) {

	DataLine newLine = new DataLine();

	GrfxMLStoreProcessor.setCloneName( path,this,newLine );

	super.getClonedElements(path,newLine);

	newLine.start = start.getClone(path);
	newLine.end = end.getClone(path);
	newLine.middle = middle.getClone(path);
	newLine.thickness = thickness.getClone(path);

	return newLine;
}
public DataPoint getEnd() {
	return end;
}
public Geometry getGeometry(GrfxMLEngine engine)
{
	if(_line==null && _translation!=null && color!=null)
	{
		_line = new GeometryLine(
				engine,
				_translation.window,
				bounds,
				_translation.visibleView,
				transparency,
				color,filled,
				_translation.positionView,
				offsetView,
				startView,endView,thicknessView
				);
		if( !NameHelper.isSystemName(getId()) )
		{
			_line.setId(getId());
		}
	}


	return(_line);
}
public GeometryLine getLine() {
	return _line;
}
public DataPoint getMiddle() {
	return middle;
}
public DataPoint getStart() {
	return start;
}
public void register(DataLinker store) {

	super.register(store);

	try{
		store.add(start,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(end,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(middle,this);
	}
	catch(LockException le)
	{}//Dummy catch

	try{
		store.add(thickness,this);
	}
	catch(LockException le)
	{}//Dummy catch
}
public void replace(Field oldObj, Field newObj) {
	replace(oldObj, newObj, false);
}
public void replace(Field oldObj, Field newObj,boolean recursive) {

	_line=null;

	super.replace(oldObj,newObj);

	if(start==oldObj)
		start=(DataPoint)newObj;

	if(end==oldObj)
		end=(DataPoint)newObj;

	if(middle==oldObj)
		middle=(DataPoint)newObj;

	if(thickness==oldObj)
		thickness=(DataSize)newObj;
}
public void resetChanged() {
	super.resetChanged();
	start.resetChanged();
	end.resetChanged();
	middle.resetChanged();
	thickness.resetChanged();
}
protected void setEnd(DataPoint newEnd) {
	end = newEnd;
}
protected void setLine(GeometryLine newLine) {
	_line = newLine;
}
protected void setMiddle(DataPoint newMiddle) {
	middle = newMiddle;
}
protected void setStart(DataPoint newStart) {
	start = newStart;
}
public String togrfxML(String name) {
	String xml = "<" + name;

	if(!NameHelper.isSystemName(getId()))
	{
		xml += " id=\""+getId()+"\"";
	}

	xml += ">";
	xml += super.togrfxML(name);
	xml += start.togrfxML(Token.START);
	xml += end.togrfxML(Token.END);
	xml += middle.togrfxML(Token.MIDDLE);
	xml += thickness.togrfxML(new DataSize(1),Token.THICKNESS,TagModifier.SIZE_TYPE_THICKNESS);
	xml += "</" + name + ">\n";
	return xml;
}
public String toString()
{
	if(GrfxMLEngine.DEBUG)
		return(togrfxML( DEFAULT_ID.substring(1) ));

	String xml = "<"+DEFAULT_ID.substring(1)+"/>";

	return xml;
}
public void translate(DataTranslation t)
{

	if(_translation==null)
		_translation=t;

	try{
		GrfxMLProcessor.middle(end,start,middle);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	try{
		GrfxMLProcessor.translate(start,TagModifier.POINT_TYPE_POINT,t,startView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	try{
		GrfxMLProcessor.translate(end,TagModifier.POINT_TYPE_POINT,t,endView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	try{
		GrfxMLProcessor.translate(thickness,TagModifier.SIZE_TYPE_THICKNESS,t,thicknessView);
	}
	catch(LockException le)
	{
		le.printStackTrace();
	}

	super.translate(t);
}
}
